package com.aivin.lib_japaneselearn;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class JapanWordAdapter extends RecyclerView.Adapter {
    private List<String []> allWord   ;
    private String footerInfo ="" ;
    private JapanwordSelectCallback japanwordSelectCallback ;

    public JapanWordAdapter( List<String []> allWord  ,String footerInfo , JapanwordSelectCallback selectCallBack) {
        this.allWord = allWord;
        this.footerInfo = footerInfo;
        this.japanwordSelectCallback = selectCallBack ;
        this.setHasStableIds(true);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(mFooterView != null && viewType == TYPE_FOOTER){
            return new FooterHolder(mFooterView);
        }

        View view   = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_japanword, parent, false);
        return new ChooseHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        int type =getItemViewType(position) ;
        if(type  == TYPE_FOOTER){
            final FooterHolder  chooseHolder= (FooterHolder)holder;
            chooseHolder.footerInfoTv.setText(footerInfo);
        }

        else if(type  == TYPE_NORMAL){
            String [] bean = allWord.get(position);
            final ChooseHolder chooseHolder = (ChooseHolder)holder;
            chooseHolder.pingjiaWord.setText( bean[0] );
            chooseHolder.pianjiaWord.setText( bean[1]);
            chooseHolder.luomaWord.setText( bean[2] );
        }
    }


    @Override
    public int getItemCount() {
        return allWord==null? 0: allWord.size();
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ChooseHolder extends RecyclerView.ViewHolder {
    private TextView pingjiaWord;
    private TextView pianjiaWord;
    private TextView luomaWord;
     private ChooseHolder(View itemView){
        super(itemView);
         pingjiaWord =  itemView.findViewById(R.id.pingjiaWord);
         pianjiaWord =  itemView.findViewById(R.id.pianjiaWord);
         luomaWord =  itemView.findViewById(R.id.luomaWord);
        View.OnClickListener clickListener = new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                int mSelectedItem = getAdapterPosition();
                japanwordSelectCallback.onSelect( allWord.get(mSelectedItem) );
            }
        };
         itemView.setOnClickListener(clickListener);
    }
}

    private class FooterHolder extends RecyclerView.ViewHolder {
        private TextView footerInfoTv;
        private FooterHolder(View itemView){
            super(itemView);
            footerInfoTv =  itemView.findViewById(R.id.footerInfoTv);
        }
    }



    /**
     * 重写这个方法，很重要，是加入Header和Footer的关键，
     * 通过判断item的类型，从而绑定不同的view
     */
    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount()-1 || mFooterView == null ){
            return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
    }

    private final int TYPE_FOOTER = 1;
    private final int TYPE_NORMAL = 2;
    private View mFooterView;

    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount()-1);
    }

    public interface JapanwordSelectCallback{
        void onSelect(String [] info );
    }

}
